package org.grow.fund.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.grow.common.exception.BillStateException;
import org.grow.common.exception.MissTargetException;
import org.grow.common.interceptor.AuthInterceptor;
import org.grow.fund.domain.BankMaterial;
import org.grow.fund.dto.BankMaterialDto;
import org.grow.fund.feign.CodeGeneratorServiceFn;
import org.grow.fund.mapper.BankMaterialMapper;
import org.grow.fund.service.BankMaterialService;
import org.grow.fund.service.CodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author: xwg
 * @CreateDate: 2023/9/25
 */

@RestController
@RequestMapping("/bankMaterial")
public class BankMaterialController {

    @Autowired
    private BankMaterialService bankMaterialService;
    @Autowired
    private BankMaterialMapper bankMaterialMapper;
    @Autowired
    private CodeGenerator codeGenerator;
    @Autowired
    private CodeGeneratorServiceFn codeGeneratorServiceFn;
    @PostMapping("/insert")
    public BankMaterial insert(@RequestBody BankMaterial bankMaterial) {
        bankMaterial.setMaterialCode(codeGeneratorServiceFn.nextCode("MK"));
//        bankMaterial.setMaterialCode(codeGenerator.nextCode("DK"));
        bankMaterial.setVersion(0);
        bankMaterial.setFundTimes(0);
        bankMaterial.setUpdateTime(LocalDateTime.now());
        String currentUser = AuthInterceptor.getCurrentUser();
        bankMaterial.setBankCode(currentUser);
        bankMaterialService.save(bankMaterial);
        return bankMaterial;
    }

    @GetMapping("/find/{code}")
    public BankMaterial find(@PathVariable("code") String code) {
        BankMaterial bankMaterialFromDb = bankMaterialService.getById(code);
        if (bankMaterialFromDb == null) {
            throw new MissTargetException(" missing bank material " + code);
        }
        return bankMaterialFromDb;
    }

    @PutMapping("/modify")
    public BankMaterial modify(@RequestBody BankMaterial bankMaterial) {
        BankMaterial bmFromDb = bankMaterialService.getById(bankMaterial.getMaterialCode());
        if (bmFromDb == null) {
            throw new MissTargetException(" missing bank material " + bankMaterial.getMaterialCode());
        }
        bmFromDb.setTitle(bankMaterial.getTitle());
        bmFromDb.setRate(bankMaterial.getRate());
        bmFromDb.setPeriod(bankMaterial.getPeriod());
        bmFromDb.setUnit(bankMaterial.getUnit());
        bmFromDb.setUpdateTime(LocalDateTime.now());
        boolean b = bankMaterialService.updateById(bmFromDb);
        if (!b) {
            throw new MissTargetException("please try again, update failed");
        }
        return bmFromDb;
    }

    @DeleteMapping("/remove/{code}")
    public String remove(@PathVariable("code") String code) {
        boolean b = bankMaterialService.removeById(code);
        if (!b) {
            throw new MissTargetException("delete failed ,maybe id is not exist");
        }
        return code;
    }

    //
    @PostMapping("/queryForBank")
    public IPage<BankMaterial> queryForBank(@RequestBody BankMaterialDto bankMaterialDto) {
        String currentUser = AuthInterceptor.getCurrentUser();
        bankMaterialDto.setBankName(currentUser);
        Page<BankMaterial> page = Page.of(bankMaterialDto.getPageNum(), bankMaterialDto.getPageSize());
        return bankMaterialMapper.dynamicQueryForBank(page, bankMaterialDto);
    }

    @PostMapping("/queryForCompany")
    public IPage<BankMaterial> queryForCompany(@RequestBody BankMaterialDto bankMaterialDto) {
        Page<BankMaterial> page = Page.of(bankMaterialDto.getPageNum(), bankMaterialDto.getPageSize());
         return bankMaterialMapper.dynamicQueryForCompany(page, bankMaterialDto);
    }

}
